package com.rem.photoablum.controller;

import com.rem.photoablum.model.Album;
import com.rem.photoablum.model.Photo;
import com.rem.photoablum.model.User;
import com.rem.photoablum.service.AlbumService;
import com.rem.photoablum.service.PhotoService;
import com.rem.photoablum.utils.PhotoUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author remembrance
 * @version 1.0
 * @date 2022/3/6 21:38
 */

@RestController
@CrossOrigin
public class PhotoController {
    @Autowired
    private PhotoService photoService;

    @Autowired
    private AlbumService albumService;


    /**
     * @desc 新建相册接口
     * 前端需要将相册名 相册描述 相册主题传到后端来
     * 相册对象  model --> Album
     * @param album
     * @param request
     */
    @PostMapping("/createAlbum")
    public void createAlbum(Album album,@RequestParam("userId") Integer userId,HttpServletRequest request){
//        User user = (User) request.getSession().getAttribute("user");
//        Integer pid = user.getId();
        Integer pid = userId;
        album.setPid(pid);
//        System.out.println("PhotoController-->:"+album);
        Album albumAfterAdd = albumService.addAlbum(album);

    }

    /**
     * 删除相册接口，传入相册的id
     * @param aid
     */
    @DeleteMapping("/deleteAlbum/{aid}")
    public void deleteAlbum(@PathVariable("aid")Long aid){
        albumService.deleteByAid(aid);
    }

    /**
     * 通过ajax请求获取当前用户下的所有相册名
     * 需要传入用户id
     * 返回所有相册对象的集合
     * @param id
     * @return
     */
    @GetMapping("/albumName/{id}")
    public List<Album> findAllAlbumNameByPid(@PathVariable("id")Integer id){
        return albumService.findAlbumByPid(id);
    }

    /**
     * @desc 获取每个相册下的所有图片 需要前端把相册的aid传到后端来
     * 通过aid去数据库中查询该相册下的所有图片
     * @param aid
     * @return
     */
    @GetMapping("/album/{aid}")
    public List<Photo> findAllByAid(@PathVariable("aid")Long aid){
        return photoService.findAllByAid(aid);
    }

    /**
     * 查询当前用户下的所有相片
     * 需传入当前用户的id
     * @param id
     * @return
     */
    @GetMapping("/photo/{id}")
    public List<Photo> findAllByPid(@PathVariable("id")Integer id){
        return photoService.findAllByPid(id);
    }


    //上传图片
    /**
     * 需要传入图片文件，以及相册名
     * @param file
     * @param albumName
     * @param request
     */
    @RequestMapping(value = "/upload")
    public void upload(@RequestParam("file") MultipartFile file,@RequestParam("albumName")String albumName,@RequestParam("userId")Integer userId,HttpServletRequest request) {
//        User user = (User) request.getSession().getAttribute("user");
//        Integer pid = user.getId();
        Integer pid = userId;
        Album album = albumService.findAlbumsByPidAndAlbumName(pid, albumName);
        photoService.upload(file,pid,album);
    }


    //文件下载
    //通过传入相片的id进行下载
    //相册对象 model --> Photo
    @GetMapping("/download/{id}")
    public String downloadFile(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response){
        return photoService.downloadFile(request,response,id);
    }

    //删除文件
    //通过传入相片的id删除
    //相片对象 model --> Photo
    @DeleteMapping("/delete/{id}")
    public String deletePhoto(@PathVariable("id")Long id){
        return photoService.deletePhoto(id);
    }


    //删除相册
    //传入相册的id删除
    //相册对象 model --> album
    @RequestMapping("/deleteAlbum/{aid}")
    public String deleteAllPhoto(@PathVariable("aid")Long aid){
        return albumService.deleteAlbum(aid);
    }

    //===================================这个接口不使用========================================
    //多文件上传
    @PostMapping("/batch")
    public String handleFileUpload(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        return photoService.handleFileUpload(request,user);
    }


}
